package org.example.springbootdeptemp.controller;

import org.example.springbootdeptemp.pojo.Dept;
import org.example.springbootdeptemp.pojo.Result;
import org.example.springbootdeptemp.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @PutMapping ("/depts")
    public Result updateDepts(@RequestBody Dept dept) {
        deptService.updateDepts(dept);
        return Result.success();
    }

    @DeleteMapping ("/depts/{id}")
    public Result updateDepts(@PathVariable Integer id) {
        deptService.deleteDepts(id);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result selectById(@PathVariable Integer id) {
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    @PostMapping("/depts")
    public Result insert(@RequestBody Dept dept) {
        deptService.insertDept(dept);
        return Result.success();
    }

    @GetMapping("/depts")
    public Result selectAll() {
        List<Dept> depts = deptService.selectAll();
        return Result.success(depts);
    }
    
    @GetMapping("/deptsemp")
    public Result selectEmp(Integer id) {
        Dept dept = deptService.selectByEmp(id);
        return Result.success(dept);
    }
}
